package pluginMain;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

public class DRIMain extends JavaPlugin implements Listener
{
	public static Plugin plugin;

	@Override
	public void onEnable()
	{
		plugin = this;
		saveDefaultConfig();
		if (DRIUtils.createTable()) {
			getLogger().info("Database connect successfuly!");
			getServer().getPluginManager().registerEvents(this, this);
		}
		else {
			getLogger().info("Database connect failed!");
			setEnabled(false);
		}
	}

	@Override
	public void onDisable()
	{
		DRISQL.closeConnect();
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
	{
		if (command.getName().equalsIgnoreCase("dri")) {
			if (sender instanceof Player) {
				DRICommand.onDri(sender);
				return true;
			}
		}

		if (command.getName().equalsIgnoreCase("driadmin")) {
			if (sender instanceof Player) {
				DRICommand.onDriadmin(sender, args);
				return true;
			}
		}
		return true;
	}

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event)
	{
		Player player = event.getPlayer();
		if (player.hasPermission("dri.use")) {
			playerJoinThread joinThread = new playerJoinThread(player);
			joinThread.start();
		}
	}

	@EventHandler
	public void onInventoryClose(InventoryCloseEvent event)
	{
		HumanEntity player = event.getPlayer();
		Inventory inventory = event.getInventory();
		if (!player.hasPermission("dri.admin")) {
			return;
		}
		if (inventory.getHolder() != null) {
			return;
		}
		if (inventory.getTitle().startsWith("DRItems")) {
			InventoryCloseThread closeThread = new InventoryCloseThread(inventory, player);
			closeThread.start();
		}
	}
}

class InventoryCloseThread extends Thread
{
	Inventory inventory;
	Player player;

	public InventoryCloseThread(Inventory inv_, HumanEntity pla_)
	{
		inventory = inv_;
		player = (Player) pla_;
	}

	@Override
	public void run()
	{
		if (DRIUtils.setItemsData(inventory)) {
			player.sendMessage(ChatColor.GREEN + "Setting successfully!");
		}
		else {
			player.sendMessage(ChatColor.RED + "Setting failed.");
		}
	}
}

class playerJoinThread extends Thread
{
	Player player;

	public playerJoinThread(Player pla_)
	{
		player = pla_.getPlayer();
	}

	@Override
	public void run()
	{
		DRIUtils.getPlayerTime(player);
		double time = DRIUtils.getTime(player);
		if (time < 0) {
			player.sendMessage(ChatColor.GREEN + "You can recive a gift by /dri command now.");
		}
		else {
			player.sendMessage(ChatColor.RED + "Next gift will spawn in " + time + " h");
		}
	}
}
